<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }



</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加销售计划</legend>
        </fieldset>

        <form class="layui-form " >

            <input type="hidden" name="guid" id="guid" lay-verify="required" :value="saleplan.guid">
            <div class="layui-form-item">
                <label class="layui-form-label">月度</label>
                <div class="layui-input-block">
                    <input type="text" name="monthly" id="monthly" lay-verify="required" :value="saleplan.monthly" placeholder="请输入月度" autocomplete="off" class="layui-input" >
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="depart" id="depart" :value="saleplan.depart">
                    <select name="departid" lay-verify="required" id="departid" lay-filter="departid">
                        <option value="">请选择部门</option>
                        <option v-for="(item, index) in departments" :value="item.guid" :selected="item.guid == saleplan.departid" :key="index" >{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">重量</label>
                <div class="layui-input-inline">
                   <input type="text" name="weight" lay-verify="required" placeholder="请输入重量" autocomplete="off" class="layui-input" :value="saleplan.weight">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'EditSalePlan',
        props: ['id'],
        data () {
            return {
                departments:[],
                guid: this.id,
                saleplan: {}
            }
        },
        mounted() {

            this.loadDepart();
            this.getDepartName()
            //this.getCurrencyEmnu();
            let that = this;

            layui.use('laydate', function(){
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#monthly', //指定元素
                    format: 'yyyy-MM'
                });
            });


            


           
            layui.use(['form'], function () {
                var form = layui.form,
                layer = layui.layer;
                let loader = layer.load();

             $.ajax({
                    url: '/sales/Saleplan!get.action',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                       
                        if (res.info != null) {
                             that.saleplan = res.info;
                              console.log(that.saleplan)
                            // that.dcode = that.substr3(that.adata.code);
                            // that.loadOrganization(that,res.info.orgguid);
                            // that.loadDepart(res.info.orgguid);
                            // //that.checkIsUpdate(res.info.code);
                            // form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });


            form.render();
                form.on('submit(submit)', function (fdata) {

                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Saleplan!update.action',
                        data: fdata.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            console.log("save",res)
                            if (res.info == "ok") {
                                that.toastSuccess("添加成功");
                                that.$parent.switchPage('SalesPlan');
                            } else {
                                that.toastText(res.info);
                            } 
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
        },
        
        updated: function () {
            layui.form.render('select');
            layui.form.render();

        },
        methods: {
            getDepartName: function (){
              var form = layui.form, layer = layui.layer;
                form.on('select(departid)', function(data){
                $("#depart").val(data.elem[data.elem.selectedIndex].text)
                })
            },
            
            loadDepart(){
                var that = this;
                $.ajax({
                    url: '/sales/Department!listAll.action',
                    type: "post",
                    dataType: "json",
                    data:{status : 1,
                            flag : 1},
                    success: function(res) {
                        if (res.info != 'error' ) {
                            that.departments = res.info;
                            //layui.form.render("select");
                        } else {
                            that.toast("获取部门数据失败");
                        }
                    },
                    complete: function () {

                    }
                });
            }
        }
    }
</script>
